package com.daigoudao.web.action;

import java.io.File;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.daigoudao.config.ConfigManager;
import com.daigoudao.util.Constants;
import com.daigoudao.util.FileUtil;
import com.daigoudao.web.common.BaseAction;
import com.daigoudao.web.model.UserAttrModel;
import com.daigoudao.web.model.UserModel;
import com.daigoudao.web.service.IUserAttrService;

public class UserAttrAction  extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6101988601017879455L;
	private static Logger logger = LoggerFactory.getLogger(UserAttrAction.class);
	private UserAttrModel userAttr;
	private File cardFile;
	private String cardFileFileName;
	private IUserAttrService userAttrService;
	UserModel userModel;
	private String t;
	
	public String getT() {
		return t;
	}

	public void setT(String t) {
		this.t = t;
	}

	public UserModel getUserModel() {
		return userModel;
	}

	public void setUserModel(UserModel userModel) {
		this.userModel = userModel;
	}

	public void setUserAttrService(IUserAttrService userAttrService) {
		this.userAttrService = userAttrService;
	}

	public File getCardFile() {
		return cardFile;
	}

	public void setCardFile(File cardFile) {
		this.cardFile = cardFile;
	}

	public String getCardFileFileName() {
		return cardFileFileName;
	}

	public void setCardFileFileName(String cardFileFileName) {
		this.cardFileFileName = cardFileFileName;
	}

	public UserAttrModel getUserAttr() {
		return userAttr;
	}

	public void setUserAttr(UserAttrModel userAttr) {
		this.userAttr = userAttr;
	}
	
	public String queryAccCheck() {
		if(!super.isLogin()){
			return "login";
		}
		try {
			userAttr=userAttrService.getUserAttr(super.getUserLogin().getUserid());
		} catch (Exception e) {	
			logger.error("getUserAttr Exception",e);
		}
		userModel=super.getUserLogin();
		if("2".equals(t)){
			return "BEAGENT";
		}
		return SUCCESS;
	}
	
	
	public String cardCheck() {
		if(!super.isLogin()){
			return "login";
		}
		Date d = new Date();
		String card = cardFileFileName.substring(0,
				cardFileFileName.indexOf("."));
		String dname = card + super.getUserLogin().getUserid()+d.getTime();
		dname = dname
				+ cardFileFileName.substring(cardFileFileName.indexOf("."));
		
		File dstFile=FileUtil.getFileDirOrCreate(ConfigManager.getConfigString(Constants.UPLOAD_IMG_DIR)+"\\"+Constants.UPLOAD_IMG_CARD_DIR, dname);
		try {
			FileUtil.copy(cardFile, dstFile);
		} catch (Exception e) {
			logger.error("Copy File Error!",e);
		}
		String cardUrl="/"+Constants.UPLOAD_IMG_CARD_DIR+"/"+dname;
		userAttr.setCardimage(cardUrl);
		userAttr.setUserid(super.getUserLogin().getUserid());
		userAttr.setIscheck(0);
		try {
			userAttrService.saveCardInfo(userAttr);
		} catch (Exception e) {
			logger.error("saveCardInfo Exception",e);
			return "checkfail";
		}
		if("2".equals(t)){
			return "BEAGENT";
		}
		return SUCCESS;
	}

}
